package com.tabnet.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

import com.tabnet.beans.produit.Option;
import com.tabnet.beans.produit.Produit;
import com.tabnet.dao.DAOException;
import com.tabnet.dao.DAOFactory;
import com.tabnet.dao.DAOOption;
import com.tabnet.dao.DAOOptionCMD;
import com.tabnet.dao.DAOUtilitaire;

public class OptionDAOImpl implements DAOOption {
	
	private static final String SQL_LISTER  		= "SELECT Ref_OPT, Description_OPT, PxO FROM Options o, ConfigurerOPT c "
			 										+ "WHERE o.Ref_OPT = c.OPT_Ref AND PRD_Ref = ?";
	private static final String SQL_LISTER_TOUT		= "SELECT Ref_OPT, Description_OPT, PxO FROM Options o";
	private static final String SQL_LIER  			= "INSERT INTO ConfigurerOPT VALUES (?, ?, ?)";
	private static final String SQL_UPDATE			= "UPDATE Options SET Description_OPT=?, PxO=? WHERE Ref_OPT=?";
	
    private DAOFactory daoFactory;
    private DAOOptionCMD _DaoOptionCMD;
	
    public OptionDAOImpl( DAOFactory daoFactory ) {
        this.daoFactory = daoFactory;
        this._DaoOptionCMD = this.daoFactory.getOptionCMDDAO();
    }
    

	@Override
	public HashMap<String, Option> listerOption(String refProd) throws DAOException {
    	Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        HashMap<String, Option> lesOptions = new HashMap<String, Option>();
        Option option;

        try {
            connection = daoFactory.getConnection();
            // récupération des fournissseurs
            preparedStatement = DAOUtilitaire.initialisationRequetePreparee( connection, SQL_LISTER, false,
            		refProd);
            resultSet = preparedStatement.executeQuery();
            while ( resultSet.next() ) {
            	option = map( resultSet );
            	lesOptions.put(option.getRef(), option);
            }
        } catch ( SQLException e ) {
            throw new DAOException( e );
        } finally {
            DAOUtilitaire.fermeturesSilencieuses( resultSet, preparedStatement, connection );
        }

        return lesOptions;
    }

    private static Option map( ResultSet resultSet ) throws SQLException {
    	Option option = new Option();
    	
    	option.setRef( resultSet.getString( "Ref_OPT" ) );
    	option.setDescription( resultSet.getString( "Description_OPT" ) );
    	option.setPxO(resultSet.getDouble( "PxO" ));
    
        return option;
    }


	@Override
	public HashMap<String, Option> listerOption() throws DAOException {
		Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        HashMap<String, Option> lesOptions = new HashMap<String, Option>();
        Option option;

        try {
            connection = daoFactory.getConnection();

            preparedStatement = DAOUtilitaire.initialisationRequetePreparee( connection, SQL_LISTER_TOUT, false);
            resultSet = preparedStatement.executeQuery();
            while ( resultSet.next() ) {
            	option = map( resultSet );
            	lesOptions.put(option.getRef(), option);
            }
        } catch ( SQLException e ) {
            throw new DAOException( e );
        } finally {
            DAOUtilitaire.fermeturesSilencieuses( resultSet, preparedStatement, connection );
        }

        return lesOptions;
	}


	@Override
	public void lierOption(Produit prod) throws DAOException {
		Connection connexion = null;
        ResultSet valeursAutoGenerees = null;
        
        try {
            connexion = daoFactory.getConnection();
            // pour chaque ligne des options :
    		for(Option opt : prod.getLstOptionP().values()){
    			//insertion
    			PreparedStatement preparedStatement = DAOUtilitaire.initialisationRequetePreparee( connexion, SQL_LIER, true,
    	        		 prod.getRef(), opt.getRef(), opt.getChoix());
                preparedStatement.executeUpdate();
                preparedStatement.close();
    		}
        } catch ( SQLException e ) {
            throw new DAOException( e );
        } finally {
        	PreparedStatement preparedStatementF = null;
        	DAOUtilitaire.fermeturesSilencieuses( valeursAutoGenerees, preparedStatementF, connexion );
        }
	}


	@Override
	public void updOption(Option pOption) throws DAOException {
		Connection oConnexion = null;
        PreparedStatement oPreparedStatement = null;
        ResultSet valeursAutoGenerees = null;

        try {
        	oConnexion = this.daoFactory.getConnection();
        	
        	//mise a jour de l'option
        	oPreparedStatement = DAOUtilitaire.initialisationRequetePreparee( oConnexion, SQL_UPDATE, true,
        			pOption.getDescription(), pOption.getPxO(), pOption.getRef());
        	
            if ( oPreparedStatement.executeUpdate() == 0 )
                throw new DAOException( "Échec de la mise a jour de l'option, aucune ligne modifiée dans la table." );

        	//insertion de cette modification dans la table des options commandes
            this._DaoOptionCMD.addOption(pOption);
        }
        catch ( SQLException e ) {
            throw new DAOException( e );
        }
        finally {
        	 DAOUtilitaire.fermeturesSilencieuses( valeursAutoGenerees, oPreparedStatement, oConnexion );
        }
	}

}
